package com.mycompany.project.server;




import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import marketplace.componente.usuarios.AdminUsuariosRemote;
import marketplace.objects.ClienteBO;
import marketplace.objects.ProveedorBO;
import marketplace.objects.UsuarioBO;

import com.mycompany.project.client.AdminUsuariosService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class AdminUsuariosServiceImpl extends RemoteServiceServlet implements AdminUsuariosService {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void registrarUsuario(UsuarioBO nUsuario, String tipo) {
		// TODO Auto-generated method stubContext ctx;
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			admin.registrarUsuario(nUsuario, tipo);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}  

		
	}

	@Override
	public void actualizarInfoUsuario(UsuarioBO nUsuario, String tipo) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			admin.actualizarInfoUsuario(nUsuario, tipo);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		
	}

	@Override
	public boolean validarUsuario(String login, String password) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			return admin.validarUsuario(login, password);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return false;
	}

	@Override
	public UsuarioBO consultarUsuario(String login) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			return admin.consultarUsuario(login);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return null;
	}

	@Override
	public ProveedorBO consultarProveedor(String login) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			return admin.consultarProveedor(login);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return null;
	}

	@Override
	public ClienteBO consultarCliente(String login) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			return admin.consultarCliente(login);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return null;
	}

	@Override
	public void eliminarUsuario(String login) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			admin.eliminarUsuario(login);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		
	}

	@Override
	public void eliminarProveedor(String login) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			admin.eliminarProveedor(login);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		
	}

	@Override
	public void eliminarCliente(String login) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			admin.eliminarCliente(login);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		
	}

	@Override
	public ArrayList<ProveedorBO> consultarProveedoresPorTipoServicio(
			String tipoServicio) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			return admin.consultarProveedoresPorTipoServicio(tipoServicio);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return null;
	}

	@Override
	public ArrayList<UsuarioBO> consultarUsuariosPorParametro(String param,
			String valor) {
		// TODO Auto-generated method stub
		Context ctx;
		try {
			ctx = new InitialContext();
			//NamingEnumeration<NameClassPair> stuff = ctx.list("");
			//while(stuff.hasMore()){
			//NameClassPair na = stuff.next();
			//System.out.println(na.getClassName()+" "+na.getName());
			//}
			AdminUsuariosRemote admin = (AdminUsuariosRemote) ctx.lookup("java:global/EnterpriseMarketplaceProject/ComponenteUsuarios/AdminUsuarios");
			return admin.consultarUsuariosPorParametro(param, valor);
			
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			System.out.println("aqui fue");
			e.printStackTrace();
			
		}
		return null;
	}

	
}
